window._MFD = {

    //计算闪避 false--命中  true--闪避
    getMiss:function(attackData,targetData){
        if(attackData.hit<targetData.miss){
            //console.log('命中闪避',attackData.hit,targetData.miss);

            var missData = targetData.miss-attackData.hit;

            //闪避极限
            if(missData>_CS.maxPer){
                missData=_CS.maxPer;
            }

            //console.log('闪避比例',missData);

            return (Math.random()*100)<missData;
        }

        return false;
    },

    //计算真实防御
    getRealDefend:function(attackData,targetData,isMagic){

        var defend = isMagic?targetData.mdefend:targetData.defend;
        var defendReduce = isMagic?attackData.mdefendReduce:attackData.defendReduce;
        var defendReduceMiss = isMagic?targetData.mdefendReduceMiss:targetData.defendReduceMiss;

        return (defendReduce>defendReduceMiss)?
        Math.floor(defend*(1-(defendReduce-defendReduceMiss)/100))
        :defend;
    },

    //计算单次伤害 isMagic-- true 法术伤害  false --物理伤害
    getDamage:function(attacker,targeter,isMagic,skillFtype){

        if(
            _STATUSHELPERCHESS.hasStatus(targeter,'xu')
            ||_STATUSHELPERCHESS.hasStatus(attacker,'xu')
        ){
            return {damage:0,isMiss:false,isSuper:false,isGedang:false,isMagic:isMagic,isxw:true};
        }

        var attackData = _ROLEDATA.getRoleDataOfPlayer(attacker);
        var targetData = _ROLEDATA.getRoleDataOfPlayer(targeter);

        var dmin = 1;
        var dmax = 1;
        var damage = 0;
        var isMiss = false;
        var isSuper = false;
        var isGedang = false;
		var isDeath = attackData.death?Math.random()<(attackData.death/100):false;
        var isNodeath = targetData.nodeath;
        var isLianji = attackData.lianji&&attackData.lianji>0;
        var gushangData = attackData.gushang;

        if(attacker.equip&&attacker.equip.length>0){
            for(var i=0;i<attacker.equip.length;i++){
                var armname = attacker.equip[i].type;
                if(armname&&_EQUIP[armname]){
                    var armtype = _EQUIP[armname].type;
                    var armtypeConfig = _CS.equipFloat[armtype];
                    if(armtypeConfig){
                        dmin = armtypeConfig.min;
                        dmax = armtypeConfig.max;
                        //console.log('装备类型:'+armtype,'浮动:',dmin,dmax);
                        break;
                    }
                }
            }
        }

        //计算miss
        isMiss = _MFD.getMiss(attackData,targetData);

        if(!isMiss){

            var realAttack = isMagic?attackData.mattack:attackData.attack;
			
			//计算致命一击
			if(isDeath&&!isMagic&&!isNodeath&&_CS.deathmax>realAttack){

                //var deathdamage =   attackData.lv>100?
                //                    Math.floor((attackData.lv)/100)*_CS.deathmax
                //                    :_CS.deathmax;
                var deathdamage = _CS.deathmax;

				return {damage:deathdamage,isMiss:isMiss,isSuper:true,isGedang:isGedang};
			}
			

            
            
            //真实防御
            var realDefend = _MFD.getRealDefend(attackData,targetData,isMagic);
            //增伤比例
            var damagePlus = attackData.damagePlus-targetData.damageReduce;

            //减伤极限
            if(damagePlus<-_CS.maxPer){
                damagePlus=-_CS.maxPer;
            }

            //console.log('减伤比例:',damagePlus,damage);

            if(isLianji){
                //console.log('携带连击伤害修正',_CS.lianjiPer);
                damage = Math.floor((realAttack*realAttack/(realAttack+realDefend))*(1+damagePlus/100)*_CS.lianjiPer);
            }else{
                //console.log('未携带连击伤害修正');
                damage = Math.floor((realAttack*realAttack/(realAttack+realDefend))*(1+damagePlus/100));
            }
            
            if(damage<0){

                damage=0;

            }else{

                //计算五行
                var attackFtype = attackData.ftype;
                if(skillFtype){
                    attackFtype=skillFtype;
                }
                var targetFtype = targetData.ftype;
                var attackFtypeConfig = _CS.ftype[attackFtype];
                var ftypeper = 1;
                var ftypeplus = attackData[attackFtypeConfig.plusAttr];
                var ftypereduce = targetData[attackFtypeConfig.reduceAttr];
                var ftypeDamagePlus = ftypeplus-ftypereduce;

                //五行抗性极限
                if(ftypeDamagePlus<-_CS.maxPer){
                    ftypeDamagePlus=-_CS.maxPer;
                }

                //console.log('五行抗性比例:',ftypeDamagePlus,damage);
                
                if(attackFtypeConfig.kz == targetFtype){
                    ftypeper=1+_CS.ftypeper;
                }else if(attackFtypeConfig.bkz == targetFtype){
                    ftypeper=1-_CS.ftypeper;
                }

                damage = Math.floor(damage*ftypeper*(1+ftypeDamagePlus/100));

                //console.log('五行抗性比例计算后:',damage);

                if(damage<0){
                    damage=0;
                }

                //计算伤害浮动
                damage = Math.floor(damage*(dmin+Math.random()*(dmax-dmin)));

                //计算暴击
                if(
                    attackData.superAttack>targetData.superAttackMiss
                    &&(Math.random()<=(attackData.superAttack-targetData.superAttackMiss)/100)
                ){

                    isSuper =true;

                    var baseper = _CS.bjper;
                    
                    baseper = baseper+(attackData.superDamagePlus-targetData.superDamageReduce)/100;

                    if(baseper<0){
                        baseper=0;
                    }
                    
                    damage = Math.floor(damage*(1+baseper));

                }

                //计算格挡
                if(targetData.gedang>0
                &&Math.random()<targetData.gedang/100
                ){
                    isGedang = true;
                    damage=Math.floor(damage*_CS.gedangper);
                }


            }

        }

        //最小伤害为1
        if(damage<=0&&!isMiss){
            damage=1;
        }

        //附加固伤
        if(gushangData){
            damage+=gushangData;
        }

        return {damage:damage,isMiss:isMiss,isSuper:isSuper,isGedang:isGedang,isMagic:isMagic};
    },


    //计算单次技能伤害
    getDamageOfSkill:function(attacker,targeter,skillname){

        if(_SKILL[skillname]){
            
            var attackerData = _ROLEDATA.getRoleDataOfPlayer(attacker);
            var skillConfig = _SKILL[skillname];
            var skilldamage = skillConfig.damage;
            var skillPer = skillConfig.per;
            var ismagic = skillConfig.ismagic;
            var skillftype = skillConfig.ftype;

            //状态加成
            if(
                skillConfig.damagestatus
                &&skillConfig.damageper
                &&_STATUSHELPERCHESS.hasStatus(attacker,skillConfig.damagestatus)
            ){
                skilldamage = skilldamage*skillConfig.damageper;
            }

            if(attackerData.magicPer>0){
                skillPer+=attackerData.magicPer/100;
            }

            //console.log('技能命中',skillConfig.per,"技能命中修正",attackerData.magicPer,"最终命中",skillPer);

            if(!skillPer||Math.random()<skillPer){
                var damageResult = _MFD.getDamage(attacker,targeter,ismagic,skillftype);
                //console.log('技能伤害',damageResult);
                if(!damageResult.isMiss){
                    damageResult.damage = Math.floor(damageResult.damage*(skilldamage/100));
                }
                return damageResult;
            }else{
                return {damage:0,isMiss:true};
            }
        }

        return {damage:0};
    },

    

    //计算回血量
    getHpAddOfSkill:function(attacker,skillname){

        if(_SKILL[skillname]&&(_SKILL[skillname].bhp||_SKILL[skillname].php)){
            
            var skillConfig = _SKILL[skillname];
            var bhp = skillConfig.bhp?skillConfig.bhp:0;
            var php = skillConfig.php?skillConfig.php:0;
            var hpmax = _ROLEDATA.getRealAttr(attacker,'maxHp');
            return bhp+Math.floor(php/100*hpmax);
        }

        return 0;
    },

    //计算回蓝量
    getMagicAddOfSkill:function(attacker,skillname){

        if(_SKILL[skillname]&&(_SKILL[skillname].bmg||_SKILL[skillname].pmg)){
            
            var skillConfig = _SKILL[skillname];
            var bmg = skillConfig.bmg?skillConfig.bmg:0;
            var pmg = skillConfig.pmg?skillConfig.pmg:0;
            var magicmax = _ROLEDATA.getRealAttr(attacker,'maxMagic');
            return bmg+Math.floor(pmg/100*magicmax);
        }

        return 0;
    },

    //计算单次道具伤害
    getDamageOfItem:function(attacker,targeter,itemname){

        var targetData = _ROLEDATA.getRoleDataOfPlayer(targeter);

        var attackData = _ROLEDATA.getRoleDataOfPlayer(attacker);

        var itemConfig = _FIGHTITEM[itemname];

        //友军道具不会miss
        if(itemConfig&&itemConfig.target=="friend"){
            return {damage:0,isMiss:false};
        }

        var per = itemConfig.per?itemConfig.per:1;

        if(attackData.aqPer){
            per+=attackData.aqPer/100;
        }

        var damage = itemConfig.damage?itemConfig.damage:0;

        //计算暗器增伤
        if(attackData.aqDamage>0){
            damage = Math.floor(damage*(1+attackData.aqDamage/100));
        }

        var isMiss = Math.random()>per||_MFD.getMiss(attackData,targetData);

        return {damage:damage,isMiss:isMiss};
    },

    //计算道具回血量
    getHpAddOfItem:function(targeter,itemname){

        var itemConfig = _FIGHTITEM[itemname];
        var maxhp = _ROLEDATA.getRealAttr(targeter,'maxHp');
        var bhp = itemConfig.bhp?itemConfig.bhp:0;
        var php = itemConfig.php?Math.floor(itemConfig.php*maxhp/100):0;
        return bhp+php;
    },

    //计算道具回蓝
    getMagicAddOfItem:function(targeter,itemname){

        var itemConfig = _FIGHTITEM[itemname];
        var maxMagic = _ROLEDATA.getRealAttr(targeter,'maxMagic');
        var bmg = itemConfig.bmg?itemConfig.bmg:0;
        var pmg = itemConfig.pmg?Math.floor(itemConfig.pmg*maxMagic/100):0;
        return bmg+pmg;
    },

    //计算实际恢复能量
    addEg:function(target,targeteg){

        if(
            targeteg<0
            &&_ROLEDATA.getRealAttr(target,'strongEnergy')>0
        ){
            return;
        }

        if(targeteg){
            target.eg += targeteg;
            if(target.eg<0){
                target.eg=0;
            }
        }

    },

    //计算实际回血
    addHp:function(targeter,hp){

        if(_STATUSHELPERCHESS.hasStatus(targeter,'jin')){
            return 0;
        }

        var targetData = _ROLEDATA.getRoleDataOfPlayer(targeter);
        var oldhp = targeter.hp;
        targeter.hp+=hp;
        if(targeter.hp>targetData.maxHp){
            targeter.hp=targetData.maxHp;
        }

        if(targeter.hp<0){
            targeter.hp = 0;
            targeter.eg = 0;
            targeter.statusArr=[];
        }else if(oldhp<=0){

            //console.log('复活');
            if(targetData['dun']>0){
                _FAM.addPlayerStatusArr(['dang_1'],targeter);
            }

        }


        return targeter.hp - oldhp;

    },

    //计算实际回蓝
    addMagic:function(targeter,magic){
        var targetData = _ROLEDATA.getRoleDataOfPlayer(targeter);
        var oldmagic = targeter.mg;
        targeter.mg+=magic;
        if(targeter.mg>targetData.maxMagic){
            targeter.mg=targetData.maxMagic;
        }

        if(targeter.mg<0){
            targeter.mg = 0;
        }


        return targeter.mg - oldmagic;

    },

    //濒死判断是否能变身
    //bdskillOnly 是否仅判断被动技能
    canBianshen:function(targeter,bdskillOnly){

        //console.log('判定是否能变身',targeter);

        if(targeter.skill&&targeter.skill.length>0){

            var skillObj = null;
            var maxlevel = -1;

            for(var i=0;i<targeter.skill.length;i++){

                var tempskillname = targeter.skill[i];

                var tempSkillObj = _SKILLBD[tempskillname];

                //非濒死判断主动技能 tempSkillObj
                if(!tempSkillObj){
                    if(!bdskillOnly){
                        tempSkillObj = _SKILL[tempskillname];
                    }
                }

                if(
                    tempSkillObj
                    &&tempSkillObj.animals
                    &&(
                        !targeter.historyAnimals
                        ||!targeter.historyAnimals[tempSkillObj.animals]
                    )
                ){

                    if(
                        _ANIMALS[tempSkillObj.animals]
                        &&_ANIMALS[tempSkillObj.animals].level>maxlevel
                    ){
                        maxlevel = _ANIMALS[tempSkillObj.animals].level;
                        skillObj = tempSkillObj;
                    }
                    
                }

            }

            if(skillObj){
                var playerNode = _CHESSFIGHT.getPlayerNodeByCode(targeter);
                _MCAC.doConceAnimals(skillObj,targeter,playerNode);
                return true;
            }

        }

        return false;

    },

    //计算损血
    cutHp:function(targeter,hp,attackData){

        var hasPoge = (attackData!=null)&&attackData.poge>0;

        if(hp>0){

            if(_STATUSHELPERCHESS.checkAndReduceStatus(targeter,"dong")){
                return 0;
            }

            if(_STATUSHELPERCHESS.checkAndReduceStatus(targeter,"dun")){
                return 0;
            }

            if(_STATUSHELPERCHESS.checkAndReduceStatus(targeter,"jue")){
                var oldplayerhp = targeter.hp;
                targeter.hp=0;
                return oldplayerhp;
            }

            //解除睡眠
            _STATUSHELPERCHESS.checkAndReduceStatus(targeter,"mian");

            if(
                !hasPoge
                &&_ROLEDATA.getRealAttr(targeter,'magicCloth')
                &&targeter.mg>0
            ){

                targeter.mg=Math.floor(targeter.mg/100);
                
				return 0;
			}

            if(
                !hasPoge
                &&_ROLEDATA.getRealAttr(targeter,'goldCloth')
                &&window.gameData.gold>0
            ){

                var realDamage = 0;
                
                if(window.gameData.gold>=hp){
                    window.gameData.gold-=hp;
                    return 0;
                }else{
                    realDamage = hp - window.gameData.gold;
                    window.gameData.gold=0;
                    targeter.hp-=realDamage;
                    return realDamage;
                }
			}
        }

        var oldhp = targeter.hp;
        targeter.hp-=hp;
        if(targeter.hp<=0){

            //灵魂状态下不会死亡
            if(_STATUSHELPERCHESS.hasStatus(targeter,"hun")){
                targeter.hp=1;
                return oldhp-1;
            }

            targeter.statusArr=[];
            if(!_MFD.canBianshen(targeter,true)){
                targeter.hp = 0;
                targeter.eg = 0;   
                targeter.mg = 0;
            }else{
                return oldhp;
            }
        }

        return targeter.hp-oldhp;
    },


    debuffPerUpdate:function(attacker,per){

        //高难度模式降低debuff命中几率
        if(attacker.isplayer&&_HDLVHELPER.getHdLv()!=0){
            var debuffper = _HDLVHELPER.getAttrByHdLv('debuffPer');
            per = per*debuffper;
            //console.log('debuff几率修正',debuffper,per);
        }else{
            //console.log('简单难度debuff无修正');
        }

        return per;
    },

    //计算技能&道具是否命中
    //技能&道具命中 && 角色命中
	//true--命中  false--闪避
    isHit:function(attacker,targeter,skillname){

        var configObj = _SKILL[skillname]?_SKILL[skillname]:_FIGHTITEM[skillname];

        var per = configObj.per?configObj.per:1;

        //per = _MFD.debuffPerUpdate(attacker,per);

        var targetData = _ROLEDATA.getRoleDataOfPlayer(targeter);

        var attackData = _ROLEDATA.getRoleDataOfPlayer(attacker);

        if(attackData.magicPer>0){
            per+=attackData.magicPer/100;
        }

        return (!_MFD.getMiss(attackData,targetData))&&Math.random()<per;
    },

    //逃跑是否成功
    isCanRun:function(player){

        //if(window.fightData
        //    &&window.fightData.fightParam
        //    &&window.fightData.fightParam.type=='hideStory'){
            //console.log('剧情战斗逃跑必定失败');
        //    return false;
        //}

        if(RSH.haskunbang(player.statusArr)){
            //console.log('困守状态必定逃跑失败');
            return false;
        }

        if(player.isplayer&&_GAMEDATAHELPER.hasTeamSkill('jipao')){
            //console.log('玩家逃跑');
            return true;
        }

        return Math.random()<0.3;

    },

    //是否有足够的魔法释放技能
    isMagicEnough:function(player,skillname){

        if(_SKILL[skillname]&&player.mg>=_SKILL[skillname].cut)
            return true;

        return false;
    }


}